home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / ImageFXDevKit.lha / sdev / sas / examples / savers / savepbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-01  |  3.5 KB  |  163 lines

  1. /*
  2.  * PBM Saver for ImageFX
  3.  *
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  8. #include <libraries/iffparse.h>
  9. #include <devices/clipboard.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <string.h>
  13.  
  14. #include <scan/modall.h>
  15. #include <scan/loadsave.h>
  16.  
  17. enum {
  18.    B_SavePBM,
  19.    TXT_COUNT
  20. };
  21.  
  22. /*
  23.  * SM_SaveTrue() - Write out a 24-bit true color image file, no palette.
  24.  */
  25.  
  26. BOOL __saveds __asm SM_SaveTrue (register __a0 char *fname,
  27.                                  register __a1 struct Buffer *buf,
  28.                                  register __d0 int id,
  29.                                  register __a2 LONG *args)
  30. {
  31.    UBYTE fbuf[256];
  32.    struct BIO *handle;
  33.    int i, j;
  34.    UBYTE *red, *grn, *blu;
  35.    BOOL grey;
  36.  
  37.    BeginBar (GetStr(B_SavePBM, "Save PBM"), buf->Height, TRUE);
  38.  
  39.    handle = BOpen(fname, MODE_NEWFILE, 0);
  40.    if (handle == NULL) { EndBar(NULL); return(FALSE); }
  41.  
  42.    SetError(ERR_Write);
  43.  
  44.    grey = (buf->Depth == 1);
  45.    if (grey) {
  46.       msprintf (fbuf, "P5\n%ld\n%ld\n255\n", buf->Width, buf->Height);
  47.    }
  48.    else {
  49.       msprintf (fbuf, "P6\n%ld\n%ld\n255\n", buf->Width, buf->Height);
  50.    }
  51.    if (BWrite (handle, fbuf, strlen(fbuf)) < strlen(fbuf)) goto err;
  52.  
  53.    for (j = 0; j < buf->Height; j++) {
  54.       if (Bar(j)) {
  55.          SetError(ERR_UserCancel);
  56.          goto err;
  57.       }
  58.       GetBufLine(buf, &red, &grn, &blu, j);
  59.       for (i = 0; i < buf->Width; i++) {
  60.          if (!BPutc(handle, *red++)) goto err;
  61.          if (!grey) {
  62.             if (!BPutc(handle, *grn++)) goto err;
  63.             if (!BPutc(handle, *blu++)) goto err;
  64.          }
  65.       }
  66.    }
  67.  
  68.    BClose(handle);
  69.  
  70.    EndBar(NULL);
  71.  
  72.    return(TRUE);
  73.  
  74. err:
  75.    BClose(handle);
  76.    DeleteFile(fname);
  77.    EndBar(NULL);
  78.    return(FALSE);
  79. }
  80.  
  81. /*
  82.  * SM_SaveMapped() - Write out a color mapped image file, with
  83.  *                   palette information.
  84.  */
  85.  
  86. BOOL __saveds __asm SM_SaveMapped (register __a0 char *fname,
  87.                                    register __a1 struct MappedImage *img,
  88.                                    register __d0 int id)
  89. {
  90.    return(FALSE);
  91. }
  92.  
  93.  
  94. /*
  95.  * SM_SavePalette() - Write out only 24-bit palette information.
  96.  */
  97.  
  98. BOOL __saveds __asm SM_SavePalette (register __a0 char *fname,
  99.                                     register __a1 struct Palette *palette,
  100.                                     register __d0 int id)
  101. {
  102.    return(FALSE);
  103. }
  104.  
  105.  
  106. static
  107. struct SaveFormat saveformats[] = {
  108.    { "PBM", SAV_TRUE | SAV_NOMASK,   0 },
  109.    { NULL }
  110. };
  111.  
  112. struct SaveFormat * __saveds SM_Signatures (void)
  113. {
  114.    return (saveformats);
  115. }
  116.  
  117.  
  118. /**********************************************************************\
  119.  
  120.                Library Functions and Initialization Stuff
  121.  
  122. \**********************************************************************/
  123.  
  124. extern ULONG LibOpen (void);
  125. extern ULONG LibClose (void);
  126. extern ULONG LibExpunge (void);
  127. extern ULONG LibNull (void);
  128.  
  129. ULONG FuncTable[] = {
  130.    /* These four MUST be present */
  131.    (ULONG) LibOpen,
  132.    (ULONG) LibClose,
  133.    (ULONG) LibExpunge,
  134.    (ULONG) LibNull,
  135.  
  136.    (ULONG) 0,
  137.    (ULONG) SM_SaveTrue,
  138.    (ULONG) SM_SaveMapped,
  139.    (ULONG) SM_SavePalette,
  140.    (ULONG) SM_Signatures,
  141.  
  142.    /* End with -1L */
  143.    (ULONG) -1L
  144. };
  145.  
  146. UBYTE LibraryID[]    = "$VER: PBM Saver 1.03.01 (12.11.92)";
  147. UBYTE LibraryType    = NT_SAVER;
  148.  
  149. long __saveds __stdargs UserOpen (struct ModuleBase *modbase)
  150. {
  151.    modbase->NumGads = 0;
  152.    modbase->NewGad = NULL;
  153.    modbase->Language = "Saver_PBM";
  154.    modbase->LangCount = TXT_COUNT;
  155.    return(TRUE);
  156. }
  157.  
  158. long __saveds __stdargs UserClose (struct ModuleBase *modbase)
  159. {
  160.    return(TRUE);
  161. }
  162.  
  163.